
package com.hboxs.asl.controller.asl.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.asl.BaseController;
import com.hboxs.asl.entity.Order;
import com.hboxs.asl.entity.OrderItem;
import com.hboxs.asl.entity.Review;
import com.hboxs.asl.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Create by xxx on 12/06/2016
 * Controller - 评论
 */
@Controller("reviewController")
@RequestMapping("/weChat/review")
public class ReviewControl extends BaseController {

    @Resource(name = "reviewServiceImpl")
    private ReviewService reviewService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "orderItemServiceImpl")
    private OrderItemService orderItemService;
    @Resource(name = "orderServiceImpl")
    private OrderService orderService;
    @Resource(name = "shippingServiceImpl")
    private ShippingService shippingService;

    /**
     * 进入订单详情页面
     *
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Long id, ModelMap modelMap) {
        try {
            Order order = orderService.find(id);
            modelMap.addAttribute("order", order);
            modelMap.addAttribute("shipping",shippingService.findByOrder(order));
            modelMap.addAttribute("orderItems",orderItemService.findOrderItemsWithoutEvaluation(order));
            return "/yijing/myOrders/evaluate-orderItem";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/yijing/404";
    }

    /**
     * 创建评论
     *
     * @param review
     * @param oId    订单项id
     * @return
     */
    @RequestMapping(value = "/review", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage review(Review review, Long oId, HttpServletRequest request) {
        try {
            OrderItem orderItem = orderItemService.find(oId);
            if (orderItem == null) {
                return JsonMessage.error("订单项不存在！");
            }
            reviewService.create(memberService.getCurrent(), orderItem, request.getRemoteAddr(), review);
            orderItem.setHasReview(true);
            orderItemService.update(orderItem);
            return JsonMessage.success("评价成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonMessage.UNKNOWN_ERROR;
    }
}